<?php

namespace App\Http\Controllers\Agent;


use Illuminate\Http\Request;
use  App\Http\Controllers\Controller;
use App\Rad_manage;
use App\Common\ErrorDefine;
use App\Common\MyCrypt;
use App\Recharge;
use App\Http\Controllers\Wxpay\WxPayController;
use App\Http\Controllers\AliPay\AliPayRequestController as AliPay;

class RechargeController extends Controller
{

    private  $mycrtpy;

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {

    }


    /**
     * @param Request $request
     * 代理微信充值
     */
    public function WxRecharge(Request $request)
    {
        //  验证用户
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }

        $token = $request->input('token');

        $user = self::getTokenById($token);

        if($user == NULL)
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }


        if(!$request->has('money') || empty($request->input('money')) || $request->input('money') < 0 || !is_int($request->input('money')) )
        {
            $money = 100;
        }
        $money = (int)$request->input('money');


        if(!$request->has('order_no') || empty($request->input('order_no')))
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }
        $order_no = $request->input('order_no');




        $params = [];
        $params['order_no'] = $order_no;
        $params['recharge_money'] = self::getArrivalMoney($money,$user);     //  到账金额
        $params['user_id'] = $user->manage_id;                         //  用户ID
        $params['pay_channel'] = 3;                                    //  微信支付通道
        $params['status'] = 0;                                         //  支付通道
        $params['created_at'] = time();                                //  创建时间
        $params['updated_at'] = time();                                //  更新时间


        $recharges = Recharge::insertGetId($params);
        $wxpay = new  WxPayController();

        $url = $wxpay->getWxpayCode($body='牛魔科技',$attach='recharge',$out_trade_no=$order_no,$total_fee=$money,$Goods_tag='牛魔科技-账户充值',$product_id=$order_no);
        return self::ShowMessage($url);


    }


    /**
     * @param $money
     * @return float|int
     *
     * 实际到账金额
     */
    static function getArrivalMoney($money,$user)
    {
        $arrival_money = 0;

        if($user->origin_type == 2)
        {
            switch ($money)
            {
                case $money >= 300 && $money < 1000  : $arrival_money = $money+ $money*0.15;break;
                case $money >= 1000 && $money < 2000 : $arrival_money = $money+ $money*0.3;break;
                case $money >= 2000 && $money < 5000 : $arrival_money = $money+ $money*0.4;break;
                case $money >= 5000 && $money < 10000 : $arrival_money = $money+ $money*0.5;break;
                case $money >= 10000 && $money < 30000 : $arrival_money = $money+ $money*0.6;break;
                case $money >= 30000 : $arrival_money = $money + $money*1;break;
                default: $arrival_money = $money;break;
            }
            return $arrival_money;
        }else{
            return $money;
        }

    }


    /**
     * @param Request $request
     * 代理支付宝充值
     */
    public function AliRecharge(Request $request)
    {
        //  验证用户
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }

        $token = $request->input('token');

        $user = self::getTokenById($token);

        if($user == NULL)
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }


        if(!$request->has('money') || empty($request->input('money')) || $request->input('money') < 0 || !is_int($request->input('money')) )
        {
            $money = 100;
        }
        $money = (int)$request->input('money');


        if(!$request->has('order_no') || empty($request->input('order_no')))
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }
        $order_no = $request->input('order_no');

        $params = [];
        $params['order_no'] = $order_no;
        $params['recharge_money'] = self::getArrivalMoney($money,$user);     //  到账金额
        $params['user_id'] = $user->manage_id;                         //  用户ID
        $params['pay_channel'] = 2;                                    //  支付宝支付通道
        $params['status'] = 0;                                         //  支付通道
        $params['created_at'] = time();                                //  创建时间
        $params['updated_at'] = time();                                //  更新时间
        $pay_terminal = 'pc';

        $recharges = Recharge::insertGetId($params);
        $alipay = new  AliPay();

        $subject = "账户充值";
        $body="牛魔科技。";
        $extra_common_param = 'recharge';

        $data = $alipay->SendRequest($order_no,$money,$pay_terminal,$body,$subject,$extra_common_param);
        return self::ShowMessage($data);


    }


    /**
     * @param Request $request
     * 检测充值状态
     */
    public function CheckRechargeStatus(Request $request)
    {
        if(!$request->has('token') || empty($request->input('token')))
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }

        $token = $request->input('token');

        $manage = Rad_manage::where('token',$token)->first();

        if($manage == NULL)
        {
            self::ShowError(ErrorDefine::INVALID_PARAM);
            return;
        }

        $recharge = Recharge::where('user_id',$manage->mange_id)->where('pay_channel',3)->ordreBy('created_at','desc')->first();

        if($recharge->status == 1)
        {
            $status = 'SUCCESS';
            return self::ShowMessage($status);
        }else{
            $status = 'FALSE';
            return self::ShowMessage($status);
        }


    }
    
    


    /**
     * @param $token
     * @return mixed
     * token   换取 用ID
     */
    static function getTokenById($token)
    {
        $user = Rad_manage::where('token',$token)->first();
        return $user;
    }




}




